package com.ruoyi.file.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.exception.ServiceException;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.file.service.ISysFileService;
import com.ruoyi.system.api.domain.SysFile;

/**
 * 文件请求处理
 * @author breeze
 * 传入参数@PathVariable,@RequestParam,默认对象接值的格式类似为/1,2,3,4,5时,会自动转为数组
 */
@RestController
public class SysFileController extends BaseController{

	private static final Logger log = LoggerFactory.getLogger(SysFileController.class);

	@Autowired
	private ISysFileService sysFileService;

	/**
	 * 文件上传请求
	 */
	@PostMapping("upload")
	public R<SysFile> upload(MultipartFile file, boolean updateSupport) {
		try {
			return R.ok(sysFileService.uploadFile(file),"文件上传成功.");
		} catch (Exception e) {
			log.error("上传文件失败", e);
			return R.failed(e.getMessage());
		}
	}

	/**
	 * 批量文件上传请求
	 */
	@PostMapping("uploadBeach")
	public R<List<SysFile>> upload(@RequestParam("files") MultipartFile[] files) {
		if (ArrayUtils.isEmpty(files)) {
			throw new ServiceException("文件不允许为空");
		}
		try {
			List<SysFile> collect = new ArrayList<>();
			for (MultipartFile file : files) {
				collect.add(sysFileService.uploadFile(file));
			}
			return R.ok(collect,"文件上传成功.");
		} catch (Exception e) {
			log.error("上传文件失败", e);
			return R.failed(e.getMessage());
		}
	}

	@GetMapping("{downType:download|inline}/{fileId}")
	public void download(@PathVariable long fileId,@PathVariable String downType,HttpServletResponse response) throws IOException {
		sysFileService.download(fileId,downType,response);
	}

    @GetMapping("/list")
    public TableDataInfo<?> list(SysFile sysFile){
        startPage();
        List<SysFile> list = sysFileService.selectSysFileList(sysFile);
        return getDataTable(list);
    }

    @GetMapping("/listByFileIds")
    public R<List<SysFile>> listByFileIds(Long[] fileIds){
        List<SysFile> list = sysFileService.selectSysFileListByFileIds(fileIds);
        return R.ok(list, "获取文件成功");
    }


    @GetMapping(value = "/{fileId}")
    public R<SysFile> getInfo(@PathVariable("fileId") Long fileId){
        return R.ok(sysFileService.selectSysFileByFileId(fileId));
    }

    @Log(title = "文件管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysFile sysFile){
        return toAjax(sysFileService.save(sysFile));
    }

    @PutMapping
    public AjaxResult edit(@RequestBody SysFile sysFile){
        return toAjax(sysFileService.updateById(sysFile));
    }

    @Log(title = "文件管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fileIds}")
    public AjaxResult remove(@PathVariable Long[] fileIds) {
        return toAjax(sysFileService.removeByIds(Arrays.asList(fileIds)));
    }
}
